//
//  PhoneLoginVC.m
//  LoginTest
//
//  Created by 战立军 on 2017/3/8.
//  Copyright © 2017年 战立军. All rights reserved.
//

#import "PhoneLoginVC.h"
#import "VerifyCodeVC.h"

@interface PhoneLoginVC ()
{
    UIButton *_selectPrefixButton;
    UITextField *_phoneText;
    
    UIButton *_sureButton;
    
    CGFloat _myheight;
    CGFloat _headheight;
}
@end

@implementation PhoneLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景色
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIUTils colorFromHexCode:@"0489c2"].CGColor,(__bridge id)[UIUTils colorFromHexCode:@"009c93"].CGColor];
    gradientLayer.locations = @[@0.3,@1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.0);
    gradientLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
    
    [self addContentView];
    [self setNav];
    
    // Do any additional setup after loading the view.
}
- (void)setNav
{
    //标题
    self.navigationItem.title = @"手机号登录";
    
    //导航栏颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏颜色
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"uitabr"]]];
    
    //返回
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"qz_icon_facade_back_light_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPress)];
    leftButton.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)backButtonPress
{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addContentView
{
    _myheight = 50;
    _headheight = 64+20;
    if (IS_IPHONE_5) {
        _myheight = 50;
        _headheight = 64+20;
    }
    
    if (IS_IPHONE_6) {
        _myheight = 50;
        _headheight = 64+25;
    }
    if (IS_IPHONE_6P) {
        
        _myheight = 65;
        _headheight = 64+30;
    }
    
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, _headheight, [UIUTils getWindowWidth], _myheight)];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:baseView];
    
    //前缀
    _selectPrefixButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectPrefixButton.frame = CGRectMake(20, 0, baseView.frame.size.width/5.0, baseView.frame.size.height);
    [_selectPrefixButton setTitle:@"+86" forState:UIControlStateNormal];
    [_selectPrefixButton setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"uitabr"]] forState:UIControlStateNormal];
    _selectPrefixButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    _selectPrefixButton.enabled = NO;
    [baseView addSubview:_selectPrefixButton];
    
    //那根线
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_selectPrefixButton.frame)+10, 0, 2, baseView.frame.size.height)];
    line.backgroundColor = [UIUTils colorFromHexCode:@"#d9d9d9"];
    [baseView addSubview:line];
    
    //手机号
    _phoneText = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(line.frame)+5, 10, baseView.frame.size.width- CGRectGetMaxX(line.frame)-5-20, baseView.frame.size.height-10*2)];
    _phoneText.placeholder = @"请输入手机号码";
    _phoneText.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneText.keyboardType = UIKeyboardTypeNumberPad;
    [_phoneText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [baseView addSubview:_phoneText];
    
    //下一步
    
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton.frame = CGRectMake(15, CGRectGetMaxY(baseView.frame)+20, [UIUTils getWindowWidth]-15*2, _myheight);
    _sureButton.backgroundColor = [UIUTils colorFromHexCode:@"#d9d9d9"];
    _sureButton.layer.cornerRadius = 10;
    [_sureButton setTitle:@"下一步" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(nextButtonPress) forControlEvents:UIControlEventTouchUpInside];
    _sureButton.enabled = NO;
    [self.view addSubview:_sureButton];
    
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


/**
 选择国家，暂未开放
 */
- (void)seletPrefixButtonPress
{}


- (void)nextButtonPress
{
    [self.view endEditing:YES];
    
    VerifyCodeVC *verifyCodeVC = [[VerifyCodeVC alloc] init];
    verifyCodeVC.userPhone = _phoneText.text;
    [self.navigationController pushViewController:verifyCodeVC animated:YES];
    
    //判断手机号
    
    //判断手机是否存在数据库
    
    //发送短信
    
    //成功后跳转页面
    
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidChange :(UITextField *)theTextField
{
    NSLog( @"text changed: %@", theTextField.text);
    if ([UIUTils isPhoneNumber:[NSString stringWithFormat:@"%@",theTextField.text]])
    {
        _sureButton.backgroundColor = [UIUTils colorFromHexCode:@"#00aaee"];
        _sureButton.enabled = YES;
    }
    else
    {
        _sureButton.backgroundColor = [UIUTils colorFromHexCode:@"#d9d9d9"];
        _sureButton.enabled = NO;
    }
}

@end
